//
//  KZWaveCountView.m
//  lg_zhinengbeikao_english
//
//  Created by 朱凯 on 2018/12/20.
//  Copyright © 2018年 zhukai. All rights reserved.
//

#import "KZWaveCountView.h"

#import "KZWaveView.h"

@implementation KZWaveCountView
{
    CAReplicatorLayer *_replicatorLayer;    // 雷达
    CAShapeLayer *_dotLayer;                // 虚线
    KZWaveView *_waveView;                  // 水波纹
    UILabel *_countLabel;                   // 数量
    UILabel *_titleLabel;                   // 标题（我的错题）
}

- (void)dealloc {
    
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}

- (instancetype)init {
    if (self = [super init]) {
        
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(startAnimation) name:UIApplicationWillEnterForegroundNotification object:nil];
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(stopAnimation) name:UIApplicationDidEnterBackgroundNotification object:nil];
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(startAnimation) name:@"KZWaveCountViewStartAnimation" object:nil];
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(stopAnimation) name:@"KZWaveCountViewStopAnimation" object:nil];


        
        [self setupUI];
    }
    return self;
}
- (void)setupUI {
    
    _waveView = [KZWaveView new];
    _waveView.backgroundColor = kBlueColor;
    _waveView.amplitudes = @[@5];
    _waveView.colors = @[UIColor.whiteColor];
    _waveView.layer.borderColor = UIColor.whiteColor.CGColor;
    _waveView.layer.borderWidth = 2;
    _waveView.risePercents = @[@0.7];
    _waveView.riseAnimateds = @[@NO];
    _waveView.speeds = @[@0.8];
    [self addSubview:_waveView];
    [_waveView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(self);
    }];
    
    _countLabel = [[UILabel alloc] init];
    _countLabel.text = @"888";
    _countLabel.font = FONT(30);
    _countLabel.textAlignment = NSTextAlignmentCenter;
    _countLabel.textColor = kPurpleColor;
    [self addSubview:_countLabel];
    [_countLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.center.equalTo(self);
    }];
    
    _titleLabel = [[UILabel alloc] init];
    _titleLabel.text = @"我的错题";
    _titleLabel.font = FONT(12);
    _titleLabel.textAlignment = NSTextAlignmentCenter;
    _titleLabel.textColor = [UIColor lightGrayColor];
    [self addSubview:_titleLabel];
    [_titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(self);
        make.bottom.mas_equalTo(-10);
    }];
    
    _dotLayer = [CAShapeLayer layer];
    _dotLayer.lineDashPattern = @[@3, @1];
    _dotLayer.lineWidth = 1;
    _dotLayer.strokeColor = kLineColor.CGColor;
    _dotLayer.fillColor = UIColor.clearColor.CGColor;
    [self.layer addSublayer:_dotLayer];

}
- (void)layoutSubviews {
    
    [super layoutSubviews];
    
    _waveView.layer.cornerRadius = 50;
    _waveView.layer.masksToBounds = true;

    if (!_replicatorLayer) {
        _replicatorLayer = [CAReplicatorLayer layer];
        _replicatorLayer.frame = self.layer.bounds;
        _replicatorLayer.instanceCount = 3;
        _replicatorLayer.instanceDelay = 2.0/3;
        [self.layer insertSublayer:_replicatorLayer atIndex:0];
        
        CAShapeLayer *shapeLayer = [CAShapeLayer layer];
        shapeLayer.fillColor = [UIColor redColor].CGColor;
        shapeLayer.opacity = 0.2;
        shapeLayer.strokeColor = [UIColor clearColor].CGColor;
        UIBezierPath *path = [UIBezierPath bezierPathWithOvalInRect:[self startRect]];
        shapeLayer.path = path.CGPath;
        [_replicatorLayer addSublayer:shapeLayer];
        
        [self startAnimation];
        
        
        UIBezierPath *dotpath = [UIBezierPath new];
        [dotpath moveToPoint:CGPointMake(30, self.height - 20)];
        [dotpath addLineToPoint:CGPointMake(self.width - 30, self.height - 20)];
        _dotLayer.path = dotpath.CGPath;
    }
}
- (CGRect)startRect {
    return CGRectMake(0, 0, self.width, self.width);
}
- (CGRect)endRect {
    return CGRectMake(-self.width * 0.2, -self.width * 0.2, self.width * 1.4, self.width * 1.4);
}


- (void)startAnimation {
    
    CAShapeLayer *layer = (CAShapeLayer *)_replicatorLayer.sublayers.lastObject;

    CABasicAnimation *ani = [CABasicAnimation animationWithKeyPath:@"path"];
    ani.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseOut];
    ani.toValue = (__bridge id)([UIBezierPath bezierPathWithOvalInRect:[self endRect]].CGPath);
    
    CABasicAnimation *ani2 = [CABasicAnimation animationWithKeyPath:@"opacity"];
    ani2.toValue = @(0);
    
    CAAnimationGroup *g = [CAAnimationGroup animation];
    g.duration = 2;
    g.repeatCount = HUGE;
    g.animations = @[ani, ani2];
    
    [layer addAnimation:g forKey:@"ani"];
}
- (void)stopAnimation {
    
    CAShapeLayer *layer = (CAShapeLayer *)_replicatorLayer.sublayers.lastObject;
    [layer removeAllAnimations];
}
@end
